package com.qianfeng.smsplatform.monitor.event;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by Jackiechan on 2021/10/11/21:33
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Component
public class Push2MailListener {

    @EventListener
    @Async
    public void onEvent(QueueLimitEvent event) throws Exception {

        //登录我们的邮箱,创建会话
        Properties properties = new Properties();

        properties.put("mail.smtp.auth", "true");
        properties.put("mail.host", "smtp.qq.com");
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.auth", "true");
        MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
        socketFactory.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", socketFactory);

        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("5433708@qq.com", "noghjlbfbdufbheh");
            }
        });

        session.setDebug(true);

        Transport transport = session.getTransport();
        transport.connect();
        InternetAddress address = new InternetAddress("qq275251839@126.com");
       // InternetAddress address1 = new InternetAddress("qq275251839@126.com");
        InternetAddress[] addresses = {address};
        //InternetAddress[] addresses = {address,address1};

        Message message = new MimeMessage(session);
        message.setSubject("队列超限预警");
        message.setFrom(new InternetAddress("5433708@qq.com"));
        message.setRecipient(Message.RecipientType.TO,address);
        message.setContent("消息队列id:"+event.getQueueId()+"中的消息数量超出了阈值,当前数量是:"+event.getCurrentCount(),"text/html;charset=utf-8");
        transport.sendMessage(message,message.getAllRecipients());
        transport.close();
    }
}
